package com.bhzyxy.myqueen;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.appcompat.widget.AppCompatTextView;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;

import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        AppCompatTextView tv_register = findViewById(R.id.tv_register);
        AppCompatTextView tv_login = findViewById(R.id.tv_login);
        AppCompatEditText et_account = findViewById(R.id.et_account);
        AppCompatEditText et_password = findViewById(R.id.et_password);


        OkGo.getInstance().init(getApplication());

        tv_register.setOnClickListener(v -> startActivity(new Intent(MainActivity.this, RegisterActivity.class)));

        tv_login.setOnClickListener(view -> {
            HttpParams httpParams = new HttpParams();
            httpParams.put("account", et_account.getText().toString());
            httpParams.put("password", et_password.getText().toString());
            OkGo.<String>post("https://shopai.yokey.top/api/mobile/index.php?w=login").tag(this).params(httpParams).execute(new StringCallback() {
                @Override
                public void onSuccess(Response<String> response) {
                    String data = response.body();
                    try {
                        JSONObject jsonObject = new JSONObject(data);
                        String code = jsonObject.getString("code");
                        if (!code.equals("1")) {
                            Log.d("TAGS", response.body());
                            Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            });
        });
    }
}
